<?php
namespace Admin\Controller;
use User\Api\UserApi;

class LinkController extends AdminController {
    public function index(){
        $nickname       =   I('nickname');
        $map['status']  =   array('egt',0);
        if(is_numeric($nickname)){
            $map['id|title']=   array(intval($nickname),array('like','%'.$nickname.'%'),'_multi'=>true);
        }else{
            $map['title']    =   array('like', '%'.(string)$nickname.'%');
        }

        $list   = $this->lists('Link',$map,'id');
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '友情链接';
        $this->display();
    }

    public function del(){
        $id = I('get.id');
        $res = D('Link')->where(array('id'=>$id))->delete();
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }



    public function add(){
        if(IS_POST){
            $Link = D("Link");
            $res = $Link->create(I("post."));
            //dump($res);
            if($res){
                if($Link->add($res)){
                    $this->success('添加成功',U('index'));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->error(D("Link")->getError());
            }
        }else{
            $this->display();
        }
    }

    public function update(){
        if(IS_POST){
            $Link = D("Link");
            $res = $Link->create(I("post."));
            if($Link->save($res)){               
                $this->success('修改成功',U('index'));
            }else{
                $this->error('修改失败');          
            }
        }else{
            $Link = D("Link");
            //$res = $Link->where(I("get.id"))->find();
            $res =$Link->where(array('id'=>I("get.id")))->find();
            //dump($Link->getlastsql());
            //dump($res);
            $this->assign('res',$res);
            $this->display();
        }
    }

    public function changeStatus($method=null){
        $id = array_unique((array)I('id',0));
        if(in_array(C('USER_ADMINISTRATOR'), $id)){
            $this->error("不允许对超级管理员执行该操作!");
        }
        $id = is_array($id) ? implode(',',$id) : $id;
        if (empty($id)){
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =   array('in',$id);
        // dump(strtolower($method));die;
        switch ( strtolower($method)){
            case 'forbiduser':
                $this->forbid('Link', $map);
                break;
            case 'resumeuser':
                $this->resume('Link', $map);
                break;
            case 'deleteuser':
                $this->delete('Link', $map);
                break;
            default:
                $this->error('参数非法');
        }
    }

}